package com.cwh.taskcenter.task.converter;

import com.alibaba.fastjson.JSON;
import com.cwh.taskcenter.domain.task.model.Task;
import com.cwh.taskcenter.domain.task.model.TaskRule;
import com.cwh.taskcenter.domain.task.model.TaskStatus;
import com.cwh.taskcenter.task.model.TaskDO;

/**
 * @author cwh
 */
public class TaskConverter {

    public static TaskDO toTaskDO(Task task) {
        TaskDO taskDO = new TaskDO();
        taskDO.setId(task.getId());
        taskDO.setTaskTitle(task.getTaskTitle());
        taskDO.setTaskType(task.getTaskType());
        taskDO.setClaimType(task.getClaimType());
        taskDO.setRuleJson(JSON.toJSONString(task.getTaskRules()));
        taskDO.setStartTime(task.getStartTime());
        taskDO.setEndTime(task.getEndTime());
        taskDO.setMaxParticipate(task.getMaxParticipate());
        taskDO.setTaskStatus(task.getTaskStatus().name());
        taskDO.setCreateTime(taskDO.getCreateTime());
        taskDO.setUpdateTime(taskDO.getCreateTime());
        return taskDO;
    }


    public static Task toTask(TaskDO taskDO) {
        if (taskDO == null) {
            return null;
        }
        Task task = new Task();
        task.setId(taskDO.getId());
        task.setTaskTitle(taskDO.getTaskTitle());
        task.setTaskType(taskDO.getTaskType());
        task.setClaimType(taskDO.getClaimType());
        task.setTaskRules(JSON.parseArray(taskDO.getRuleJson(), TaskRule.class));
        task.setStartTime(taskDO.getStartTime());
        task.setEndTime(taskDO.getEndTime());
        task.setMaxParticipate(taskDO.getMaxParticipate());
        task.setTaskStatus(TaskStatus.fromString(taskDO.getTaskStatus()));
        task.setCreateTime(taskDO.getCreateTime());
        task.setUpdateTime(taskDO.getUpdateTime());
        return task;
    }

}
